package com.sanjiahantang.youjiarole.module.me.favorite.views.impl;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sanjiahantang.plugin.swipemenulistview.SwipeMenu;
import com.sanjiahantang.plugin.swipemenulistview.SwipeMenuCreator;
import com.sanjiahantang.plugin.swipemenulistview.SwipeMenuItem;
import com.sanjiahantang.plugin.swipemenulistview.SwipeMenuListView;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseBean;
import com.sanjiahantang.youjiarole.base.BaseMvpFragment;
import com.sanjiahantang.youjiarole.module.match.beans.MatchBean;
import com.sanjiahantang.youjiarole.module.me.favorite.presenters.FavoriteGoodsPresenter;
import com.sanjiahantang.youjiarole.module.me.favorite.views.FavoriteGoodsView;

/**
 * Created by zy on 2017/9/5.
 */

public class FavoriteGoodsFragment extends BaseMvpFragment<FavoriteGoodsView,FavoriteGoodsPresenter>
        implements FavoriteGoodsView,SwipeMenuCreator,SwipeMenuListView.OnMenuItemClickListener {
    private View $view ,isVisible;
    private SwipeMenuListView lvCases;
    @Override
    public void showProgress() {
        context.showProgress(context);
    }

    @Override
    public void hideProgress() {
        context.hideProgress();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if ($view != null) {
            ViewGroup parent = (ViewGroup) $view.getParent();
            if (parent != null) {
                parent.removeView($view);
            }
        } else {
            $view = inflater.inflate(R.layout.fragment_list, container, false);
        }
        return $view;
    }

    @Override
    public FavoriteGoodsPresenter initPresenter() {
        return new FavoriteGoodsPresenter();
    }

    @Override
    protected void initView() {
        lvCases = (SwipeMenuListView) $view.findViewById(R.id.lvCases);
        isVisible=$view.findViewById(R.id.isVisible);
        isVisible.setVisibility(View.VISIBLE);

        lvCases.setAdapter(presenter.getAdapter(context));


        lvCases.setMenuCreator(this);
        lvCases.setOnMenuItemClickListener(this);
    }

    @Override
    protected void initData() {
        presenter.loadData(context);
    }

    @Override
    public void create(SwipeMenu menu) {
        SwipeMenuItem deleteItem = new SwipeMenuItem(context);
        deleteItem.setBackground(new ColorDrawable(Color.RED));
        deleteItem.setWidth(Utils.dip2px(context,60));
        deleteItem.setTitle("删除");
        deleteItem.setTitleColor(Color.WHITE);
        deleteItem.setTitleSize(16);
        menu.addMenuItem(deleteItem);
    }

    @Override
    public boolean onMenuItemClick(final int position, SwipeMenu menu, int index) {
        //index的值就是在SwipeMenu依次添加SwipeMenuItem顺序值，类似数组的下标。
        //从0开始，依次是：0、1、2、3...
        switch (index) {
            case 0:
                Utils.showDialog(context, "提示", "确定删除吗？", "", "", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Utils.dismissDialog();
                        switch (view.getId()) {
                            case com.sanjiahantang.uilibrary.R.id.txtDialogCancel:
                                break;
                            case com.sanjiahantang.uilibrary.R.id.txtDialogSure:
                                presenter.deleteCollectInfo(context,((MatchBean.Goods)lvCases.getAdapter().getItem(position)).getMgiId(), new OnLoadListener() {
                                    @Override
                                    public void onLoad(Object content) {
                                        BaseBean baseBean = (BaseBean) content;
                                        Utils.showToast(context,baseBean.getMessage());
                                        presenter.loadData(context);
                                    }
                                });
                                break;
                        }
                    }
                });

                break;
        }

        // false : 当用户触发其他地方的屏幕时候，自动收起菜单。
        // true : 不改变已经打开菜单的样式，保持原样不收起。
        return false;
    }
}
